﻿using Architecture.Domain;
using Architecture.Domain.Dto;
using Architecture.Repository.Migrations;
using Architecture.Service;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Architecture.Api.Controllers
{
    /// <summary>
    /// API控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TraceController : ControllerBase
    {
        protected readonly ITraceService _traceService;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="traceService"></param>
        public TraceController(ITraceService traceService)
        {
            _traceService = traceService;
        }
        #region 溯源
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddAsync(Traceability model)
        {
            return await _traceService.AddAsync(model);
        }
        #region 上传图片
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdateFile(IFormFile file)
        {
            var fileName= file.FileName;
            var Path=Directory.GetCurrentDirectory() + @"/wwwroot/Imge/" +fileName;
            using FileStream fa = new FileStream(Path, FileMode.Create);
            file.CopyTo(fa);
            fa.Close();
            return Ok(fileName);
        }
        #endregion
        /// <summary>
        /// 溯源列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<object> GetAllAsync()
        {
            return await _traceService.GetAllAsync();
        }
        /// <summary>
        /// 商品列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<Goods>> GetGooda()
        {
            return await _traceService.GetGooda();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<int> DeleteAsync(int id)
        {
            return await _traceService.DeleteAsync(id);
        }
        /// <summary>
        /// 查看详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Traceability> GetIdAsync(int id)
        {
            return await _traceService.GetIdAsync(id);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> UpdateAsync(Traceability model)
        {
            return await _traceService.UpdateAsync(model);
        }
        #endregion

        #region 养殖
        /// <summary>
        /// 养殖添加
        /// </summary>
        /// <param name="mode"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddFarming(Farming mode)
        {
            return await _traceService.AddFarming(mode);
        }
        /// <summary>
        /// 养殖列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<object> GetFarming(int id)
        {
            return await _traceService.GetFarming(id);
        }
        #endregion

        #region 保健
        /// <summary>
        /// 保健添加
        /// </summary>
        /// <param name="mode"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddHealth(Health mode)
        {
            return await _traceService.AddHealth(mode);
        }
        /// <summary>
        /// 保健列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<object> GetHealth(int id)
        {
            return await _traceService.GetHealth(id);
        }
        /// <summary>
        /// 保健查看详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns>返回受影响行数</returns>
        [HttpGet]
        public async Task<Health> GetAllHealth(int id)
        {
            return await _traceService.GetAllHealth(id);
        }
        /// <summary>
        /// 保健修改实体
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> UpdateHealth(Health model)
        {
            return await _traceService.UpdateHealth(model);
        }
        /// <summary>
        /// 删除、真
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<int> DeleteHealth(int id)
        {
            return await _traceService.DeleteHealth(id);
        }
        #endregion

        #region 企业
        /// <summary>
        /// 企业添加
        /// </summary>
        /// <param name="mode"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddBusiness(Business mode)
        {
            return await _traceService.AddBusiness(mode);
        }
        /// <summary>
        /// 企业列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<object> GetBusiness(int id)
        {
            return await _traceService.GetBusiness(id);
        }
        /// <summary>
        /// 修改企业
        /// </summary>
        /// <param name="mode"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> UpdateBusiness(Business mode)
        {
            return await _traceService.UpdateBusiness(mode);
        }
        #endregion

        /// <summary>
        /// 产品检验报告图片
        /// </summary>
        /// <param name="mode"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddPhoto(Photo mode)
        {
            return await _traceService.AddPhoto(mode);
        }
        /// <summary>
        /// 产品检验图删除
        /// </summary>
        /// <param name="mode"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<int> DeletePhoto(string? url)
        {
            return await _traceService.DeletePhoto(url);
        }
        /// <summary>
        /// 检验图片详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<PhotoDto>> GetPhoto(int id)
        {
            return await _traceService.GetPhoto(id);
        }
        /// <summary>
        /// 动检图详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<PhotoDto>> GetDPhoto(int id)
        {
            return await _traceService.GetDPhoto(id);
        }
        /// <summary>
        /// 企业图片详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<PhotoDto>> GetQPhoto(int id)
        {
            return await _traceService.GetQPhoto(id);
        }
        /// <summary>
        /// 资质图详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<PhotoDto>> GetZPhoto(int id)
        {
            return await _traceService.GetZPhoto(id);
        }
        /// <summary>
        /// 食品图详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<PhotoDto>> GetSPhoto(int id)
        {
            return await _traceService.GetSPhoto(id);
        }
        #region 屠宰
        /// <summary>
        /// 屠宰添加
        /// </summary>
        /// <param name="mode"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddSlaughter(Slaughter mode)
        {
            return await _traceService.AddSlaughter(mode);
        }
        /// <summary>
        /// 屠宰列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<object> GetSlaughter(int id)
        {
            return await _traceService.GetSlaughter(id);
        }
        #endregion

        #region 深加工
        /// <summary>
        /// 深加工添加
        /// </summary>
        /// <param name="mode"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> Addprocessing(Processing mode)
        {
            return await _traceService.Addprocessing(mode);
        }
        /// <summary>
        /// 深加工显示
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<object> Getprocessing(int id)
        {
            return await _traceService.Getprocessing(id);
        }
        #endregion
    }
}
